	Ext.BLANK_IMAGE_URL = "../script/ext-2.0.2/resources/images/default/s.gif"; 
	
	Ext.namespace('ZANY.system');
	var MjRoot = new Ext.tree.AsyncTreeNode({id:'0',draggable:false,text:'机构树'});
	
	ZANY.system.MenuTree = Ext.extend(Ext.tree.TreePanel,{
			constructor:function(){
				ZANY.system.MenuTree.superclass.constructor.call(this , {
					checkModel: 'cascade',   //对树的级联多选 
			  		onlyLeafCheckable: false,//对树所有结点都可选 
			  		animate:true,//开启动画效果
		        	enableDD:true,
		        	useArrows:true,
        			containerScroll: true, 
		        	border:false,//没有边框		        	
			  		autoScroll:true, 
			  		singleClickExpand:false,
					loader:new Ext.tree.TreeLoader({
		  				dataUrl:'orgInfo!getList.action?dateTime='+new Date().getTime()
		  			}),
	  				root : MjRoot
	  			});
	  		},
	  		initEvents: function() {
	  			this.on('click',function(n,e){
	  				if(n.attributes.id=='0'){
	  					menuForm.form.reset();
	  				}else{
		  				menuForm.getForm().load({url:'orgInfo!getInfo.action?dateTime='+new Date().getTime()+'&orgCode='+n.attributes.id, waitMsg:'Loading'});
	  					Ext.getCmp("subButt").setText('修改');
	  				}
					menuForm.getForm().findField('parentOrg').setDisabled(true);
					menuForm.getForm().findField('orgCode').setDisabled(true);
					menuForm.getForm().findField('createdDate').setDisabled(true);
					menuForm.getForm().findField('createdBy').setDisabled(true);
				}); 
	  		}
	})
	
	Ext.reg('sys_menu', ZANY.system.MenuTree);
	
	var org_tree =new  ZANY.system.MenuTree({height:600,width:790});
		
	var org_mj = new Ext.menu.Menu({
	        id :'add_mj',
	        items : [{
		        		id:'checkuptLc',
		                text: '添加机构', 
		                iconCls: 'add',
		                handler:function(){
		                	var nodeid = org_mj.contextNode;
		                	if(nodeid=='0'){
		                		nodeid='/';
					       		addMenu(nodeid);
		                	}else if(nodeid != ''){
		                		addMenu(nodeid);
		                	}else if(nodeid ==''){
		                		Ext.MessageBox.alert('提示信息', '请先选择上级机构！'); 
		                	}
		                }
				    },{
		        		id:'del',
		                text: '删除机构',
		                iconCls:'remove',
		                handler:function(){
		                	var nodeid = org_mj.contextNode;
		                	var nodetext = org_mj.contextText;
					        delTree(nodeid,nodetext);
		                }
		            }]
				}); 
	
	function addMenu(orgid){
		menuForm.form.reset();
		orgid = orgid=='0'?"/":orgid;
		menuForm.getForm().findField('parentOrg').setValue(orgid);
		menuForm.getForm().findField('parentOrg').setDisabled(true);
		menuForm.getForm().findField('orgCode').setDisabled(false);
		menuForm.getForm().findField('createdDate').setDisabled(true);
		menuForm.getForm().findField('createdBy').setDisabled(true);
		Ext.getCmp("subButt").setText('新增');
	}
	
	function delTree(orgCode,nodetext){
		if(orgCode=='0'){
			ZANY.util.Msg('信息',"不允许直接删除所有机构！");
			return;
		}
		Ext.MessageBox.confirm('Message','请问您真的要删除机构『'+nodetext+'』么？',function(btn){
        	if(btn == 'yes'){	
				Ext.Ajax.request({url:'orgInfo!delete.action?dateTime='+new Date().getTime(),
		            params:{orgCode:orgCode},
		            success:function(response, options){
		               var response = Ext.util.JSON.decode(response.responseText);
						if(response.success){
		                	ZANY.util.Msg('信息',"删除机构成功！");
		                	MjRoot.reload();
		                	org_tree.expand();
						}else if(!response.success){
							ZANY.util.Msg('信息',"删除机构信息失败，如有问题请联系系统管理员！"+response.msg);  
						}             
		            }
		    	});
        	}
		});
	}
  	//增加右键点击事件
     org_tree.on('contextmenu',function(node,event){//声明菜单类型
          event.preventDefault();//关闭默认的菜单，以避免弹出两个菜单 
          org_mj.contextNode = node.attributes.id;
          org_mj.contextText = node.attributes.text;
          org_mj.showAt(event.getXY());//取得鼠标点击坐标，展示菜单
		  node.select();//选中点击的节点
     });
     org_tree.on('click',function(node){//声明菜单类型
		  var nodeid = node.attributes.id;
		  node.select();//选中点击的节点
     });
     
	var paramStore1 = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: '../util/multiParam!getMultiParam.action?dateTime='+new Date().getTime()+'&paramCode=SYS002'}),
        reader: new Ext.data.JsonReader({
            root: 'param',
            fields:["key","value"]
        }),
        remoteSort: true
    }); 
    
    var paramStore2 = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: '../util/multiParam!getMultiParam.action?dateTime='+new Date().getTime()+'&paramCode=SYS003'}),
        reader: new Ext.data.JsonReader({
            root: 'param',
            fields:["key","value"]
        }),
        remoteSort: true
    }); 
    
	paramStore1.load();

	paramStore2.load();
	
	var isVisibleSelect = new Ext.form.ComboBox({
						  id:'isVisibleValue',
						  name:'isVisibleValue',
						  fieldLabel: '机构状态',
					      hiddenName: 'isVisible',
					      store: paramStore1,
					      valueField:'key',
				          displayField:'value',
				          typeAhead: true,
				          mode: 'local',
				          triggerAction: 'all',
				          resizable:true,
				          emptyText:'请选择机构使用状态...',
				          allowBlank:false,
				          anchor:'95%',
				          forceSelection:true
						});
						
	var isBottomSelect = new Ext.form.ComboBox({
						  id:'isBottomValue',
						  name:'isBottomValue',
						  fieldLabel: '底层机构',
					      hiddenName: 'isBottom',
					      store: paramStore2,
					      valueField:'key',
				          displayField:'value',
				          typeAhead: true,
				          mode: 'local',
				          triggerAction: 'all',
				          resizable:true,
				          emptyText:'请选择是否底层机构...',
				          allowBlank:false,
				          anchor:'95%',
				          forceSelection:true
						});
											
	var menuForm = new Ext.FormPanel({
					id:'ext_form',
			        labelWidth: 100, 
			        waitMsg:'正在提交，请稍后...',
			        frame:true,
			        bodyStyle:'padding:5px 5px 0',
			        reader: new Ext.data.JsonReader({
							            root: 'ZANY',
							            totalProperty: "results",
							            fields:["orgCode","parentOrg","orgName","isBottom","isVisible","createdBy","createdDate","orgLvl"]
							        }),
			        items: [{
			     			layout: 'form',
			     			defaultType: 'textfield',
				            items: [{
					            		fieldLabel: '上级机构代码',
						                name: 'parentOrg',
						                maxLength: 20,
						                readOnly:true,
						                allowBlank:false,
					                    anchor:'95%'
					                },{
					            		fieldLabel: '机构代码',
						                name: 'orgCode',
						                maxLength:20,
						                allowBlank:false,
					                    anchor:'95%'
					                },{
					            		fieldLabel: '机构名称',
						                name: 'orgName',
						                maxLength:15,
						                allowBlank:false,
					                    anchor:'95%'
					                },
									isVisibleSelect,
									isBottomSelect,{
					            		fieldLabel: '创建者',
						                name: 'createdBy',
						                maxLength:30,
					                    anchor:'95%'
					                },{
					            		fieldLabel: '创建时间',
						                name: 'createdDate',
						                maxLength:30,
					                    anchor:'95%'
					                }
					                ]
			             
							}],
					buttons:[{
							id:'subButt',iconCls:'add',text: '添加',handler:submit
				        },{
				            text: '取消',iconCls:'remove',handler: function(){menuForm.form.reset();}
				        }]
				})
	
	function submit(){
		if(menuForm.form.isValid()){
			menuForm.getForm().findField('parentOrg').setDisabled(false);
			menuForm.getForm().findField('orgCode').setDisabled(false);
			menuForm.getForm().findField('createdDate').setDisabled(true);
			menuForm.getForm().findField('createdBy').setDisabled(true);
			var subTranCode = "insert";
			if(Ext.getCmp("subButt").text=='修改')subTranCode = "update";
    		menuForm.form.submit({
                url: 'orgInfo!'+subTranCode+'.action?dateTime='+new Date().getTime(),
				waitTitle:"机构管理",
                waitMsg: '正在提交数据,请稍后... ...',
                success: function(){
					MjRoot.reload();
                	org_tree.expand();
					Ext.MessageBox.alert('提示信息', Ext.getCmp("subButt").text+'机构成功！'); 
				},
				failure:function(form,action){
					Ext.MessageBox.alert('提示信息', Ext.getCmp("subButt").text+'机构失败,请联系系统管理员!'+action.result.msg);
				}
            });
		}
	}
	
	var onePanel = new Ext.Panel({
					id:'onePanel',
					split:true,
					layout: 'fit',
					minWidth: 100,
					minHeight: 100,
					title :'[机构]',
					collapsible: true,
					width:400,
					items:[org_tree],
					region:'west'
				});
	var twoPanel = new Ext.Panel({
					split:true,
					id:'twoPanel',
					minWidth: 100,
					minHeight: 100,
					collapsible: true,
					layout: 'fit',
					items:[menuForm],
					region:'center'
				});
				
	Ext.onReady(function(){
		Ext.QuickTips.init(); 
		Ext.form.Field.prototype.msgTarget = 'side';	
		new Ext.Viewport({
			layout:'border',//表格布局
			defaults:{autoScroll: true},
			items: [onePanel,twoPanel]
		});
	})